package com.durian.ui.adapter.multi;

import androidx.recyclerview.widget.DiffUtil;

import com.durian.base.utils.StringUtils;

import java.util.List;

/**
 * 说明：MultiDiffCallback
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2019-12-16 13:26
 * <p/>
 * 版本：version 1.0
 */
public abstract class MultiDiffCallback<T> extends DiffUtil.Callback {

    private List<T> oldData;
    private List<T> newData;

    public void setOldData(List<T> oldData) {
        this.oldData = oldData;
    }

    public void setNewData(List<T> newData) {
        this.newData = newData;
    }

    @Override
    public int getOldListSize() {
        return oldData == null ? 0 : oldData.size();
    }

    @Override
    public int getNewListSize() {
        return newData == null ? 0 : newData.size();
    }

    @Override
    public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
        T oldItem = oldData.get(oldItemPosition);
        T newItem = newData.get(newItemPosition);
        if (StringUtils.isEquals(oldItem.getClass().getName(),newItem.getClass().getName())){
            return areItemsTheSame(oldItem,newItem);
        }else {
            return false;
        }
    }

    @Override
    public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
        T oldItem = oldData.get(oldItemPosition);
        T newItem = newData.get(newItemPosition);
        if (StringUtils.isEquals(oldItem.getClass().getName(),newItem.getClass().getName())){
            return areContentsTheSame(oldItem,newItem);
        }else {
            return false;
        }
    }

    public abstract boolean areItemsTheSame(T oldItem,T newItem);

    public abstract boolean areContentsTheSame(T oldItem,T newItem);

}
